using UnityEngine;

public enum SlimeType
{
    Big, Medium, Small,
}

public class Slime : Enemy
{
    [Header("Slime spesific")]
    [SerializeField] private SlimeType slimeType;
    [SerializeField] private int amountOfSlimesToCreate;
    [SerializeField] private Vector2 minCreationVelocity;
    [SerializeField] private Vector2 maxCreationVelocity;
    private GameObject slimePrefab;
    public SlimeGroundState GroundState { get; private set; }
    public SlimeIdleState IdleState { get; private set; }
    public SlimeMoveState MoveState { get; private set; }
    public SlimeBattleState BattleState { get; private set; }
    public SlimeAttackState AttackState { get; private set; }
    public SlimeStunnedState StunnedState { get; private set; }
    public SlimeDeadState DeadState { get; private set; }

    protected override void Awake()
    {
        base.Awake();
        GroundState = new(this, "idle");
        IdleState = new(this, "idle");
        MoveState = new(this, "move");
        BattleState = new(this, "move");
        AttackState = new(this, "attack");
        StunnedState = new(this, "stunned");
        DeadState = new(this, "dead");
        switch (slimeType)
        {
            case SlimeType.Big:
                transform.localScale = new Vector3(1.5f, 1.5f);
                slimePrefab = Resources.Load<GameObject>("Prefabs/Enemy/SlimeMedium");
                break;
            case SlimeType.Medium:
                slimePrefab = Resources.Load<GameObject>("Prefabs/Enemy/SlimeSmall");
                transform.localScale = new Vector3(1f, 1f);
                break;
            case SlimeType.Small:
                transform.localScale = new Vector3(0.5f, 0.5f);
                slimePrefab = Resources.Load<GameObject>("Prefabs/Enemy/SlimeSmall");
                break;
        }
    }

    private void Start()
    {
        StateMachine.Initialize(IdleState);
    }

    public override bool CanBeStunned()
    {
        if (canBeStunned)
        {
            StateMachine.ChangeTo(StunnedState);
            return true;
        }
        return false;
    }

    public override void Die()
    {
        base.Die();
        StateMachine.ChangeTo(DeadState);
        if (slimeType == SlimeType.Small) return;
        CreateSlimes(amountOfSlimesToCreate, slimePrefab);
    }

    private void CreateSlimes(int amount, GameObject prefab)
    {
        for (int i = 0; i < amount; i++)
        {
            GameObject obj = Instantiate(prefab, transform.position, Quaternion.identity);
            obj.GetComponent<Slime>().SetupSlime(FacingDirection);
        }
    }

    public void SetupSlime(int direction)
    {
        if (direction != FacingDirection)
        {
            Flip();
        }
        float xVelocity = UnityEngine.Random.Range(minCreationVelocity.x, maxCreationVelocity.x);
        float yVelocity = UnityEngine.Random.Range(minCreationVelocity.y, maxCreationVelocity.y);
        isKnockbacked = true;
        GetComponent<Rigidbody2D>().velocity = new Vector2(xVelocity * direction, yVelocity);
        Invoke(nameof(CancelKnockback), 1.5f);
    }

    private void CancelKnockback() => isKnockbacked = false;
}
